﻿#region Usings

using System.Linq;
using System.Text.RegularExpressions;
using FluentValidation;
using Yandex.Market.Api.Model;

#endregion

namespace Yandex.Market.Api.Validation
{
    internal class ShopValidator: AbstractValidator<Shop>
    {
        public ShopValidator()
        {
            RuleFor(shop => shop.Name).Length(1, 20);
            RuleFor(shop => shop.Company).NotEmpty();
            RuleFor(shop => shop.Url).NotEmpty().Matches(@"^(ht|f)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&amp;%\$#_]*)?$");
            RuleFor(shop => shop.Emails).Must(emails =>
                                              emails.All(email =>
                                                         Regex.IsMatch(email,
                                                                       @"^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$")))
                .When(shop => shop.Emails != null && shop.Emails.Count > 0);
            RuleFor(shop => shop.Categories).NotEmpty().SetCollectionValidator(new CategoryValidator());
            RuleFor(shop => shop.Currencies).NotEmpty().SetCollectionValidator(new CurrencyValidator());
            RuleFor(shop => shop.Offers).NotEmpty().SetValidator(new CollectionValidator<OfferBase>(new PolymorphicChildValidator<OfferBase>()
                                                          .Add(new OfferSimpleValidator())
                                                          .Add(new OfferVendorModelValidator())
                                                          .Add(new OfferBookValidator())
                                                          .Add(new OfferArtistTitleValidator())
                                                          .Add(new OfferAudioBookValidator())
                                                          .Add(new OfferEventTicketValidator())
                                                          .Add(new OfferTourValidator())));
        }
    }
}